package com.duo.controller.user;

import com.duo.dto.ShoppingCartCacheDTO;
import com.duo.dto.ShoppingCartDTO;
import com.duo.entity.User;
import com.duo.result.BaseResult;
import com.duo.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController("userShoppingCartController")
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @return
     */
    @PostMapping("/save")
    public Object save(@RequestBody List<ShoppingCartDTO> shoppingCartDTOList,
                       HttpServletRequest request){
        log.info("添加购物车，商品信息为：{}",shoppingCartDTOList);
        User user = (User) request.getAttribute("user");
        shoppingCartService.addShoppingCart(shoppingCartDTOList, user.getId());
        return BaseResult.success("添加购物车成功");
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public Object list( HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        List<ShoppingCartCacheDTO> list = shoppingCartService.showShoppingCart(user.getId());
        return BaseResult.success("查看购物车成功", list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Object clean(HttpServletRequest request){
        User user = (User) request.getAttribute("user");
        shoppingCartService.cleanShoppingCart(user.getId());
        return BaseResult.success();
    }


}
